package com.httpclient;

/***
 * httpclient 4.1 之前的版本 设置httpclient 连接池
 */

import org.apache.commons.httpclient.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.log4j.Logger;

import com.spider.util.SpiderCondition;
import com.spider.util.SpiderProperties;

public class HttpClientManagerOld {

	private static HttpParams httpParams;
	private static ClientConnectionManager connectionManager;
	
	/**最大连接数*/
	public final static int MAX_TOTAL_CONNECTIONS = 800;
	/***获取链接的最大等待时间*/
	public final static int MAX_TIMEOUT = 60000;
	/**每个路由的最大连接数*/
	public final static int MAX_ROUTE_CONNECTIONS = 400;
	/**链接超时时间*/
	public final static int CONNECTION_TIMEOUT = 10000;
	/**读取超时时间*/
	public final static int READ_TIMEOUT = 10000;
	 
	private static final Logger log = Logger.getLogger(SpiderHttpClientManager.class);
	private static ThreadSafeClientConnManager cm = null;  
	 
	
	static {  
	        SchemeRegistry schemeRegistry = new SchemeRegistry();  
	        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory .getSocketFactory()));  
	        schemeRegistry.register(new Scheme("https", 443, PlainSocketFactory .getSocketFactory()));  
	        
	        String max = SpiderProperties.getString(SpiderCondition.HTTPCLIENT_CONNECTION_MAX_SIZE);
	        String def = SpiderProperties.getString(SpiderCondition.HTTPCLIENT_CONNECTION_DEFAULT_SIZE);
	        cm = new ThreadSafeClientConnManager(schemeRegistry);  
	        cm.setMaxTotal(Integer.parseInt(max));  
	        cm.setDefaultMaxPerRoute(Integer.parseInt(def));  
	}  
	  
	    /*** 获取 httpclient 连接
	     * @return
	     */
	    public static HttpClient getHttpClient() {  
	    	log.info("get httpclient……");
	        HttpParams params = new BasicHttpParams();  
	        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);  
	        String timeout = SpiderProperties.getString(SpiderCondition.HTTPCLIENT_CONNECTION_TIMEOUT);
	        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Integer.parseInt(timeout)); // 3000ms  
	        return new DefaultHttpClient(cm, params);  
	    } 
	    
}
